package com.woniu.shops.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.woniu.common.CustomException.UserException;
import com.woniu.common.constans.Constants;
import com.woniu.common.pojo.Flower;
import com.woniu.common.pojo.Shops;
import com.woniu.common.pojo.User;
import com.woniu.common.vo.ShopsDto;
import com.woniu.common.vo.Shopss;
import com.woniu.common.vo.UserDto;
import com.woniu.common.vo.Users;
import com.woniu.shops.mapper.ShopsMapper;
import com.woniu.shops.service.ShopsService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ShopsServiceImp implements ShopsService {
    @Resource
    private ShopsMapper shopsMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public ShopsDto logins(Shopss shopss) {
        String src = (String) redisTemplate.opsForValue().get(shopss.getUuid());
        boolean b = src.equals(shopss.getCode()) ? true : false;
        if(!b){
            throw new UserException("验证码不正确");
        }
        Shops shops = new Shops();
        shops.setSname(shopss.getSname());
        List<Shops> shopsList = shopsMapper.selectOneShops(shops);
        if(CollectionUtils.isEmpty(shopsList)){
            throw new UserException("用户名不存在");
        }
        Shops shops1 = shopsList.get(0);
        if(!shopss.getSpassword().equals(shops1.getSpassword())){
            throw new UserException("用户名或者密码错误");
        }

        Map<String, Object> map = BeanUtil.beanToMap(shops1);
        //创建token   map为我们的payload   tUser1.getUsername().getBytes()我们的密钥
        String token = JWTUtil.createToken(map, shops1.getSname().getBytes());
        //redis当中也存入一份token
        redisTemplate.opsForValue().set(Constants.TOKEN,token,1, TimeUnit.DAYS);
        ShopsDto shopsDto = new ShopsDto();
        BeanUtil.copyProperties(shops1, shopsDto);
        shopsDto.setToken(token);
        return shopsDto;
    }

    @Override
    public void add(Shops shops) {
        shopsMapper.insertUser(shops);
    }

    @Override
    public int selectShopsName(String sname) {
        try {
            Shops shops = shopsMapper.selectShopsName(sname);
            if (shops != null && shops.getSname().equals(sname)) {
                return 1; // 表示名称已存在
            }
        } catch (Exception e) {
            System.out.println("没有名字叫：" + sname);
        }
        return 0; // 表示名称不存在
    }

    @Override
    public Shops selectShop(Shops shop) {
        return shopsMapper.selectOne(shop);
    }

    @Override
    public void updateShop(Shops shop) {
        if(shop!=null){
            Integer i = shopsMapper.updateShop(shop);
            if(i > 0){
                return ;
            }else {
                throw new UserException("不好意思,修改失败");
            }
        }
        throw new UserException("不好意思,该条数据不存在");

    }

}
